﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Configuration
{
    public class GamePlayConfiguration : ConfigurationSection
    {
        private static GamePlayConfiguration instance = null;
        private static readonly object mylock = new object();

        private GamePlayConfiguration() { }

        public static GamePlayConfiguration GetInstance()
        {
            lock ((mylock))
            {
                if (instance == null)
                {
                    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
                    configFileMap.ExeConfigFilename = "Configuration/Game.config";

                    instance = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
                                    .GetSection("GamePlayConfiguration") as GamePlayConfiguration;
                }
                return instance;
            }
        }

        [ConfigurationProperty("GamePlayList")]
        public GamePlayList GamePlayList
        {
            get
            {
                return base["GamePlayList"] as GamePlayList;
            }
        }
    }

    [ConfigurationCollection(typeof(GamePlayList), AddItemName = "GamePlay", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class GamePlayList : ConfigurationElementCollection
    {
        private List<GamePlay> _GamePlay;
        public List<GamePlay> GamePlay
        {
            get
            {
                if (_GamePlay == null)
                {
                    _GamePlay = new List<GamePlay>();
                    foreach (GamePlay config in this)
                    {
                        GamePlay.Add(config);
                    }
                }
                return GamePlay;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new GamePlay();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return element as GamePlay;
        }
    }

    public class GamePlay : ConfigurationElement
    {
        [ConfigurationProperty("ID", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string ID
        {
            get
            {
                return base["ID"] as string;
            }
            set
            {
                base["ID"] = value;
            }
        }

        [ConfigurationProperty("ShowAllMobsLifeBar", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string ShowAllMobsLifeBar
        {
            get
            {
                return base["ShowAllMobsLifeBar"] as string;
            }
            set
            {
                base["ShowAllMobsLifeBar"] = value;
            }
        }
    }
}
